<?php

declare(strict_types=1);

namespace app\validate\system;

use think\Validate;

class SysPermissionValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'name' => 'max:80',
        'code' => 'max:120|regex:^[a-z0-9_.\-]+$',
        'description' => 'max:255',
        'client_type' => 'in:admin,seller,buyer,desktop,mobile',
        'status' => 'in:0,1',
        'role_ids' => 'array',
        'role_ids.*' => 'integer|gt:0',
    ];

    protected $message = [
        'id.integer' => '权限ID格式不正确',
        'id.gt' => '权限ID格式不正确',
        'name.require' => '权限名称不能为空',
        'name.max' => '权限名称不能超过80个字符',
        'code.require' => '权限编码不能为空',
        'code.max' => '权限编码不能超过120个字符',
        'code.regex' => '权限编码仅支持小写字母、数字、点、下划线及短横线',
        'description.max' => '描述长度不能超过255个字符',
        'client_type.in' => '客户端类型不正确',
        'status.in' => '状态值不正确',
        'role_ids.array' => '角色参数必须是数组',
        'role_ids.*.integer' => '角色ID必须为数字',
        'role_ids.*.gt' => '角色ID必须大于0',
    ];

    protected $scene = [
        'create' => [
            'name',
            'code',
            'description',
            'client_type',
            'status',
            'role_ids',
        ],
        'update' => [
            'id',
            'name',
            'code',
            'description',
            'client_type',
            'status',
            'role_ids',
        ],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('name', 'require')
            ->append('code', 'require|unique:auth_permission,code')
            ->append('client_type', 'require|in:admin,seller,buyer,desktop,mobile')
            ->append('status', 'in:0,1');
    }

    protected function sceneUpdate(): self
    {
        return $this->only($this->scene['update'])
            ->append('id', 'require|integer|gt:0')
            ->append('name', 'require')
            ->append('client_type', 'in:admin,seller,buyer,desktop,mobile')
            ->append('status', 'in:0,1');
    }
}
